package com.practicesuite.controllayer;

import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JLabel;
import java.awt.Dimension;

import javax.swing.ScrollPaneConstants;
import javax.swing.JList;
import java.awt.GridLayout;
import javax.swing.JButton;
import java.awt.FlowLayout;
import javax.swing.SwingConstants;

import com.practicesuite.systemlayer.Appointment;
import com.practicesuite.systemlayer.Nurse;
import com.practicesuite.systemlayer.Patient;
import com.practicesuite.systemlayer.SystemDriver;
import com.practicesuite.systemlayer.User;

import java.awt.Font;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.util.List;
import javax.swing.JTextField;
import javax.swing.ListSelectionModel;

/**
 * Home panel for nurses.
 * 
 * @author James
 *
 */
public class NurseHomePanel extends JPanel {
    private static final long serialVersionUID = 1L;
    public MainWindow window;
    private final Nurse nurse;
    
    private List<Appointment> appointments;
    private String[] visibleAppointments;
    private Appointment appointment;
    private int appointmentIndex;
    
    private List<Patient> patients;
    private JTextField textField;

    /**
     * Create the panel.
     */
    public NurseHomePanel(MainWindow container, final User user) {
        this.window = container;
        nurse = (Nurse)user;
        
        setLayout(new GridLayout(0, 1, 0, 0));
        setPreferredSize(new Dimension(500, 400));
        
        try {
            appointments = SystemDriver.getAppointments();
        } catch (Exception e) {
            JOptionPane.showMessageDialog(null, "System error, please contact administrator.");
        }
        visibleAppointments = new String[appointments.size()];
        for (int i = 0; i < appointments.size(); i++) {
            visibleAppointments[i] = appointments.get(i).getReasonForApp();
        }
        
        JPanel panel = new JPanel();
        panel.setLayout(new GridLayout(0, 1, 0, 0));
        panel.setPreferredSize(new Dimension(500, 400));
        
        JLabel lblWelcome = new JLabel("Welcome, " + nurse.getName());
        lblWelcome.setFont(new Font("Tahoma", Font.PLAIN, 24));
        lblWelcome.setHorizontalAlignment(SwingConstants.CENTER);
        panel.add(lblWelcome);
        
        JLabel lblCurrentAppointments = new JLabel(" Upcoming Appointments:");
        lblCurrentAppointments.setVerticalAlignment(SwingConstants.BOTTOM);
        lblCurrentAppointments.setFont(new Font("Tahoma", Font.PLAIN, 16));
        panel.add(lblCurrentAppointments);
        
        JPanel panel_4 = new JPanel();
        panel_4.setLayout(new FlowLayout(FlowLayout.LEFT, 5, 0));
        panel.add(panel_4);
        
        final JList appointmentList = new JList(visibleAppointments);
        appointmentList.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
        appointmentList.setFont(new Font("Tahoma", Font.PLAIN, 12));
        
        JScrollPane scrollPane_2 = new JScrollPane(appointmentList);
        scrollPane_2.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);
        scrollPane_2.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);
        scrollPane_2.setPreferredSize(new Dimension(415, 50));
        panel_4.add(scrollPane_2);
        
        JPanel panel_1 = new JPanel();
        panel.add(panel_1);
        panel_1.setLayout(new FlowLayout(FlowLayout.LEFT, 5, 5));
        
        JButton btnRequestNewAppointment = new JButton("View All Appointments");
        btnRequestNewAppointment.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent arg0) {
                window.gotoPanel("appointmentList", nurse);
            }
        });
        
        JButton btnViewAppointmentDetails = new JButton("View Appointment Details");
        btnViewAppointmentDetails.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent arg0) {
                appointmentIndex = appointmentList.getSelectedIndex();
                try {
                    appointment = appointments.get(appointmentIndex);
                    window.gotoPanel("viewAppointment", appointment, nurse);
                } catch (Exception e) {
                    JOptionPane.showMessageDialog(null, "Please select an appointment.");
                }
            }
        });
        
        btnViewAppointmentDetails.setFont(new Font("Tahoma", Font.PLAIN, 14));
        panel_1.add(btnViewAppointmentDetails);
        btnRequestNewAppointment.setFont(new Font("Tahoma", Font.PLAIN, 14));
        panel_1.add(btnRequestNewAppointment);
        
        JPanel panel_6 = new JPanel();
        FlowLayout flowLayout_1 = (FlowLayout) panel_6.getLayout();
        flowLayout_1.setHgap(0);
        flowLayout_1.setVgap(20);
        flowLayout_1.setAlignment(FlowLayout.LEFT);
        panel.add(panel_6);
        
        JLabel lblMedicalHistory = new JLabel(" Search patients: ");
        panel_6.add(lblMedicalHistory);
        lblMedicalHistory.setVerticalAlignment(SwingConstants.BOTTOM);
        lblMedicalHistory.setFont(new Font("Tahoma", Font.PLAIN, 16));
        
        textField = new JTextField();
        panel_6.add(textField);
        textField.setColumns(15);
        
        JLabel label = new JLabel(" ");
        label.setFont(new Font("Tahoma", Font.PLAIN, 14));
        panel_6.add(label);
        
        JButton btnSearch = new JButton("Search");
        btnSearch.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent arg0) {
                try {
                    patients = SystemDriver.getPatientList(textField.getText());
                } catch (Exception e) {
                    JOptionPane.showMessageDialog(null, "System error, please contact administrator.");
                    e.printStackTrace();
                }
                window.gotoPanelPatients("patientAdministration", patients, nurse);
            }
        });
        btnSearch.setFont(new Font("Tahoma", Font.PLAIN, 14));
        panel_6.add(btnSearch);
        
        JPanel panel_5 = new JPanel();
        panel_5.setLayout(new FlowLayout(FlowLayout.LEFT, 5, 5));
        panel.add(panel_5);
        
        JButton btnViewAllPatients = new JButton("View All Patients");
        btnViewAllPatients.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent arg0) {
                try {
                    patients = SystemDriver.getPatientList();
                } catch (Exception e) {
                    JOptionPane.showMessageDialog(null, "System error, please contact administrator.");
                    e.printStackTrace();
                }
                window.gotoPanelPatients("patientAdministration", patients, nurse);
            }
        });
        
        btnViewAllPatients.setFont(new Font("Tahoma", Font.PLAIN, 14));
        panel_5.add(btnViewAllPatients);
        
        JPanel panel_2 = new JPanel();
        FlowLayout flowLayout = (FlowLayout) panel_2.getLayout();
        flowLayout.setAlignment(FlowLayout.LEFT);
        panel.add(panel_2);
        
        JButton btnViewAllTreatment = new JButton("Create New Treatment Record");
        btnViewAllTreatment.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent arg0) {
                window.gotoPanel("createTreatment", nurse);
            }
        });
        
        btnViewAllTreatment.setFont(new Font("Tahoma", Font.PLAIN, 14));
        panel_2.add(btnViewAllTreatment);
        
        JButton btnViewAllTreatment_1 = new JButton("View All Treatment Records");
        btnViewAllTreatment_1.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent arg0) {
                window.gotoPanel("treatmentList", nurse);
            }
        });
        btnViewAllTreatment_1.setFont(new Font("Tahoma", Font.PLAIN, 14));
        panel_2.add(btnViewAllTreatment_1);
        
        JScrollPane scrollPane = new JScrollPane(panel);
        
        JPanel panel_3 = new JPanel();
        panel.add(panel_3);
        
        JButton btnLogOut = new JButton("Log Out");
        btnLogOut.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent arg0) {
                window.gotoPanel("login");
            }
        });
        
        btnLogOut.setFont(new Font("Tahoma", Font.PLAIN, 14));
        panel_3.add(btnLogOut);
        scrollPane.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);
        scrollPane.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);
        add(scrollPane);

    }

}
